﻿(function () {
    'use strict';
    angular.module('app').factory('catalogSrvc', ['$http', '$q', 'apiresource', 'primeData',
        function ($http, $q, apiresource, primeData) {

            return {
                getCars: getCars,
                addCars: addCars,
                getCarBrandNames: getCarBrandNames,
                getCartypes: getCartypes,
                getCarById: getCarById,
                editCar: editCar,
                deleteCarById: deleteCarById
            };

            function getCars() {
                var deferred = $q.defer();
                var url = apiresource.GetCars;

                $http({ method: 'GET', url: url})
                    .success(function (data) {
                        deferred.resolve(data);
                    })
                    .error(function (data, status) {
                        deferred.reject(data, status);
                    });
                return deferred.promise;
            }

            function addCars(cars) {
                var deferred = $q.defer();
                var url = apiresource.AddCars;
                var headers = { Authorization: 'Bearer ' + primeData.getUser().Token };
                
                $http({ method: 'POST', url: url, data: cars, headers: headers })
                    .success(function (data) {
                        deferred.resolve(data);
                    })
                    .error(function (data, status) {
                        deferred.reject(data, status);
                    });
                return deferred.promise;
            }

            function getCarBrandNames() {
                var deferred = $q.defer();
                var url = apiresource.GetBrandNameList;

                $http({ method: 'GET', url: url })
                    .success(function (data) {
                        deferred.resolve(data);
                    })
                    .error(function (data, status) {
                        deferred.reject(data, status);
                    });
                return deferred.promise;
            }

            function getCartypes() {
                var deferred = $q.defer();
                var url = apiresource.GetCarTypeList;

                $http({ method: 'GET', url: url })
                    .success(function (data) {
                        deferred.resolve(data);
                    })
                    .error(function (data, status) {
                        deferred.reject(data, status);
                    });
                return deferred.promise;
            }

            function getCarById(id) {
                var deferred = $q.defer();
                var url = apiresource.GetCars + '/' + id;

                $http({ method: 'GET', url: url })
                    .success(function (data) {
                        deferred.resolve(data);
                    })
                    .error(function (data, status) {
                        deferred.reject(data, status);
                    });
                return deferred.promise;
            }

            function editCar(car) {
                var deferred = $q.defer();
                var url = apiresource.EditCar;
                var headers = { Authorization: 'Bearer ' + primeData.getUser().Token };

                $http({ method: 'PUT', url: url, data: car, headers: headers })
                    .success(function (data) {
                        deferred.resolve(data);
                    })
                    .error(function (data, status) {
                        deferred.reject(data, status);
                    });
                return deferred.promise;
            }

            function deleteCarById(id) {
                var deferred = $q.defer();
                var url = apiresource.GetCars + '/' + id;

                $http({ method: 'DELETE', url: url })
                    .success(function (data) {
                        deferred.resolve(data);
                    })
                    .error(function (data, status) {
                        deferred.reject(data, status);
                    });
                return deferred.promise;
            }

        }]);
})();